import AttributeUtil from '@/AttributeUtil'
import BaseUtil from '@/BaseUtil'
import FramePoint from '@/FramePoint'
import MathUtil from '@/MathUtil'
import PlayerUtil from '@/PlayerUtil'
import RandomUtil from '@/RandomUtil'
import SelectUtil from '@/SelectUtil'
import { Frame } from '@/frame'
import ClosureCounter from '@sl-wc3/lib/debug/ClosureCounter'
import { GameEvents } from 'code/Core/System/Configs/EventConfig'
import EquipmentQuality from 'code/Scripts/Items/EquipmentQuality'

// '|cff00af00'

export default class TestSys {
    tipFrame: Frame
    constructor() {
        this.test()

        se.on(GameEvents.英雄选择完毕, (hero) => {
            UnitAddItemById(hero, 'Kdyh')
            UnitAddItemById(hero, 'Khyy')
            UnitAddItemById(hero, 'Kzrz')
            UnitAddItemById(hero, 'Kds1')
        })

        this.tipFrame = Frame.createTEXT(DzGetGameUI())
        this.tipFrame
            .setAbsPoint(FramePoint.bottomLeft, 0.008, 0.008)
            .setSize(0.05, 0.014)
        BaseUtil.onTimer(1, this.show.bind(this))
    }
    show() {
        this.tipFrame.setText('闭包：' + ClosureCounter.callCount)
        return true
    }
    test() {
        se.onPlayerChat(
            '-',
            (e) => {
                if (e.eventPlayerChatString.includes('-gold')) {
                    let [_, number] = e.eventPlayerChatString.split(' ')
                    PlayerUtil.addGoldState(e.triggerPlayer, S2I(number))
                }

                if (e.eventPlayerChatString.includes('-start')) {
                    se.emit('GAME_HARD_SELECTED', 1)
                }

                if (e.eventPlayerChatString.includes('-item')) {
                    let tar = SelectUtil.getAnHero(GetPlayerId(e.triggerPlayer))
                    let [_, id, number] = e.eventPlayerChatString.split(' ')
                    if (S2I(number) > 0) {
                        for (let i = 0; i < S2I(number); i++) {
                            UnitAddItemById(tar, id)
                        }
                    } else {
                        UnitAddItemById(tar, id)
                    }
                }

                if (e.eventPlayerChatString.includes('-qua')) {
                    let tar = SelectUtil.getAnHero(GetPlayerId(e.triggerPlayer))
                    let [_, id, number] = e.eventPlayerChatString.split(' ')
                    if (S2I(number) > 0) {
                        for (let i = 0; i < S2I(number); i++) {
                            const it = UnitAddItemById(tar, id)
                            EquipmentQuality.Util.randomItemQuality(it)
                        }
                    } else {
                        const it = UnitAddItemById(tar, id)
                        EquipmentQuality.Util.randomItemQuality(it)
                    }
                    // const it = UnitAddItemById(tar, 'iyld')
                }

                if (e.eventPlayerChatString.includes('-sklv')) {
                    let tar = SelectUtil.getAnHero(GetPlayerId(e.triggerPlayer))
                    UnitAddItemById(tar, 'Kdyh')
                    let it = CreateItem('afac', GetUnitX(tar), GetUnitY(tar))
                    AttributeUtil.setItemAttribute(it, { Adyh_cdp: 0.65 })
                }

                if (e.eventPlayerChatString.includes('-dmg')) {
                    let tar = SelectUtil.getAnHero(GetPlayerId(e.triggerPlayer))
                    SetUnitX(tar, 0)
                    SetUnitY(tar, 0)
                    let u = CreateUnit(Player(11), 'ewsp', 500, 500, 0)
                    AttributeUtil.addUnitAttribute(u, {
                        life: 10000,
                        生命恢复: 2000,
                    })
                    UnitAddItemById(tar, 'Kyhs')
                    let it = CreateItem('afac', GetUnitX(tar), GetUnitY(tar))
                    AttributeUtil.setItemAttribute(it, { Ayhs_inc_damage: 100 })
                }

                if (e.eventPlayerChatString.includes('-enemy')) {
                    let [_, id, c] = e.eventPlayerChatString.split(' ')
                    let count = S2I(c) == 0 ? 1 : S2I(c)
                    for (let i = 0; i < count; i++) {
                        let { x, y } = MathUtil.polarProjection(
                            500,
                            500,
                            RandomUtil.nextInt(0, 250),
                            RandomUtil.randomAngle()
                        )
                        let u = CreateUnit(Player(11), id, x, y, 0)
                    }
                }

                if (e.eventPlayerChatString.includes('-dumy')) {
                    let [_, id, c] = e.eventPlayerChatString.split(' ')
                    let count = S2I(c) == 0 ? 1 : S2I(c)
                    for (let i = 0; i < count; i++) {
                        let u = CreateUnit(e.triggerPlayer, id, 500, 500, 0)
                    }
                }

                if (e.eventPlayerChatString.includes('-shabao')) {
                    let [_, id, c] = e.eventPlayerChatString.split(' ')
                    let count = S2I(c) == 0 ? 1 : S2I(c)
                    for (let i = 0; i < count; i++) {
                        let u = CreateUnit(Player(11), id, 500, 500, 0)
                        AttributeUtil.addUnitAttribute(u, {
                            life: 10000,
                            生命恢复: 100,
                            move_speed: -1000,
                        })
                    }
                }

                if (e.eventPlayerChatString.includes('-super')) {
                    let [_, key, type, value] =
                        e.eventPlayerChatString.split(' ')
                    let u = SelectUtil.getAnHero(GetPlayerId(e.triggerPlayer))
                    let item = UnitItemInSlot(u, 0) as any
                    if (item == 0) return
                    let at = AttributeUtil.getItemAttribute(item, true)
                    let v: any = 0
                    if (type == 'int') {
                        v = S2I(value)
                    } else if (type == 'float' || type == 'percent') {
                        v = S2R(value)
                    } else {
                        v = 1
                    }
                    let obj = { [key]: v }
                    print_r(obj)
                    AttributeUtil.add(at, obj)
                }

                if (e.eventPlayerChatString.includes('-moo')) {
                    let u = CreateUnit(Player(0), 'Emoo', 0, 0, 0)
                    TalentManager.TalentMonnShoot.addTalent(u)
                }

                if (e.eventPlayerChatString.includes('-lq')) {
                    let tar = SelectUtil.getRealSelectUnit()
                    UnitResetCooldown(tar)
                    SetUnitLifePercentBJ(tar, 100)
                    SetUnitManaPercentBJ(tar, 100)
                }
            },
            false
        )
        // se.onUnitSelected(() => {
        //     let u = GetTriggerUnit();
        //     print_r(AttributeUtil.getUnitAttribute(u, true));
        // });
        /**
         *  神话    S   6
         *  传奇    A   5
         *  史诗    B   4
         *  精良    C   3
         *  优良    D   2
         *  普通    E   1
         *
         */
    }
}
